package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.Game;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

//import com.guzhichat.guzhi.activity.GameWebViewActivity;
//import com.guzhichat.guzhi.imageload.ImageLoaderClient;
//import com.guzhichat.guzhi.util.StringUtils;

/**
 * 游戏适配器
 */
public class GameMoreAdapter extends GzBaseAdapter {

    private Context mContext;
    private List<Game> list;
    private GlideUtils imageLoaderClient;
    private boolean isShowTopType = false;
    private User user;

    public GameMoreAdapter(Context context, boolean isShowTopType, User user) {
        this.mContext = context;
        this.isShowTopType = isShowTopType;
        this.user = user;
        imageLoaderClient = new GlideUtils();
    }

    public GameMoreAdapter(Context context) {
        this.mContext = context;
        imageLoaderClient = new GlideUtils();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_game_listmore, viewGroup, false);
        ViewHolder vh = new ViewHolder(view, mListener, mLongClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final GameMoreAdapter.ViewHolder viewHolder = (ViewHolder) holder;
        final Game game = list.get(position);

        viewHolder.game_index.setText(position + 1 + "");

        //显示冠军
        if (isShowTopType) {
            if (game.getMonthTop() != null && user.getId().equals(game.getMonthTop().getId())) {
                viewHolder.mGrouptype.setVisibility(View.VISIBLE);
                viewHolder.mGrouptype.setText(StringUtils.getString(R.string.gamemorecontent1));
            } else if (game.getWeekTop() != null && user.getId().equals(game.getWeekTop().getId())) {
                viewHolder.mGrouptype.setVisibility(View.VISIBLE);
                viewHolder.mGrouptype.setText(StringUtils.getString(R.string.gamemorecontent2));
            } else if (game.getDayTop() != null && user.getId().equals(game.getDayTop().getId())) {
                viewHolder.mGrouptype.setVisibility(View.VISIBLE);
                viewHolder.mGrouptype.setText(StringUtils.getString(R.string.gamemorecontent3));
            }
            viewHolder.tv_start.setText("PK");
        } else {
            viewHolder.mGrouptype.setVisibility(View.GONE);
            viewHolder.tv_start.setText(StringUtils.getString(R.string.gamemorecontent4));
        }

        viewHolder.mGroupname.setText(game.getName() + "");
        viewHolder.mGroup_info.setText(game.getPlayCount() + "人在玩");
        imageLoaderClient.Glidde_loadRoudImage(mContext,list.get(position).getLogo(),
                viewHolder.mGroupicon,12);

        if (position == 0) {
            viewHolder.mLine.setVisibility(View.GONE);
        } else {
            viewHolder.mLine.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public List<Game> getList() {
        return list;
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private ImageView mLine;
        private ImageView mGroupicon;
        private TextView mGroupname;
        private TextView mGrouptype;
        private TextView tv_start, game_index;
        private TextView mGroup_info;

        public ViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener) {
            super(view, listener, longClickListener);
            mLine = (ImageView) view.findViewById(R.id.line);
            mGroupicon = (ImageView) view.findViewById(R.id.groupicon);
            mGroupname = (TextView) view.findViewById(R.id.groupname);
            mGrouptype = (TextView) view.findViewById(R.id.grouptype);
            tv_start = (TextView) view.findViewById(R.id.tv_start);
            game_index = (TextView) view.findViewById(R.id.game_index);
            mGroup_info = (TextView) view.findViewById(R.id.group_info);
            view.setTag(R.id.tag_first);

        }
    }


    public void setList(final List<Game> list1) {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            public void run() {
                list = list1;
                if (null == list) {
                    list = new ArrayList<Game>();
                }
            }
        });
    }
}

